class Solution(object):
    def leftRightDifference(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        n = len(nums)
        res = [0] * n
        for i in range(n):
            if i == 0:
                leftSum = 0
            else:
                leftSum = sum(nums[:i]) # nums[:i]从头到i-1的元素
            if i == n-1:
                rightSum = 0
            else:
                rightSum = sum(nums[i+1:]) # nums[i+1:]从i+1到末尾的元素
            res[i] = abs(leftSum - rightSum)
        return res
if __name__ == '__main__':
    nums = [10, 4, 8, 3]
    print(Solution().leftRightDifference(nums))
# 思路：
# 遍历数组，对于每个元素，求出其左边元素的和leftSum和右边元素的和rightSum，然后求出leftSum-rightSum的绝对值，并将结果放入res数组中。
# 时间复杂度：O(n)
# 空间复杂度：O(n)
